package com.xx.frame;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationGroupManager;
import com.intellij.notification.Notifications;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.maven.project.MavenProject;
import org.jetbrains.idea.maven.project.MavenProjectsManager;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.Objects;

public class MainFrame implements ToolWindowFactory {

    private ComponentService componentService = ComponentService.getInstance();

    @Override
    public void createToolWindowContent(Project project, @NotNull ToolWindow toolWindow) {

        final ContentFactory factory = ContentFactory.SERVICE.getInstance();
        {
            JPanel outerJPanel = buildOuterJPanel();

            // 信息展示 panel（上）
            JPanel infoPanel = buildInfoJPanel();
            outerJPanel.add(infoPanel, BorderLayout.NORTH);

            // 中部 panel
            JPanel centerPanel = buildCenterJPanel(project, toolWindow);
            JScrollPane scrollPane = new JBScrollPane(centerPanel, JBScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JBScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            //鼠标滚轮对应的滚动速度。数字越大，滑动速度越大
            scrollPane.getVerticalScrollBar().setUnitIncrement(20);
            outerJPanel.add(scrollPane, BorderLayout.CENTER);

            // 左侧配置 panel
            JPanel configJPanel = buildConfigJPanel();
            this.componentService.addConfigField(configJPanel, project, centerPanel, infoPanel);
            outerJPanel.add(configJPanel, BorderLayout.WEST);

            // 按钮 panel（下）
            /*JPanel buttonJPanel = buildButtonJPanel();
            buttonJPanel.add(this.componentService.buildParseButton(project, centerPanel, infoPanel));
            buttonJPanel.add(this.componentService.buildCleanButton(project, centerPanel, infoPanel));
            buttonJPanel.add(this.componentService.buildPackageButton("mvn", "package", project, configJPanel, centerPanel, infoPanel));
            buttonJPanel.add(this.componentService.buildPackageButton("mvnd", "package mvnd", project, configJPanel, centerPanel, infoPanel));
            outerJPanel.add(buttonJPanel, BorderLayout.SOUTH);*/

            // 整体内容
            final Content content1 = factory.createContent(outerJPanel, "NO.1", false);
            toolWindow.getContentManager().addContent(content1);
        }
    }

    /**
     * 外部 JPanel
     */
    private JPanel buildOuterJPanel() {
        JPanel panel = new JPanel();
        // BorderLayout将容器分为东、西、南、北、中五个区域
        panel.setLayout(new BorderLayout());
        return panel;
    }

    /**
     * 顶部信息展示 panel
     */
    private JPanel buildInfoJPanel() {
        JPanel panel = new JPanel();
        panel.add(new JLabel("欢迎使用快速指定模块打包工具"));
        return panel;
    }

    /**
     * 左侧配置 panel
     */
    private JPanel buildConfigJPanel() {
        JPanel panel = new JPanel();
        //BoxLayout layout = new BoxLayout(panel, BoxLayout.Y_AXIS);
        panel.setLayout(new GridBagLayout());
        return panel;
    }


    private JPanel buildButtonJPanel() {
        JPanel panel = new JPanel();
        // FlowLayout‌：是JPanel和JApplet的默认布局管理器，组件按照从上到下、从左到右的顺序排列
        panel.setLayout(new FlowLayout(FlowLayout.LEFT));
        return panel;
    }

    /**
     * 中间的 panel
     */
    private JPanel buildCenterJPanel(Project project, @NotNull ToolWindow toolWindow) {
        JPanel panel = new JPanel();
        panel.setFont(new Font("宋体", Font.BOLD, 32));
        panel.setForeground(JBColor.BLUE);
        // GridLayout 将容器空间划分为行和列，每个单元格放置一个组件
        //panel.setLayout(new GridLayout(0, 1));

        // 内容靠上
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        return panel;
    }

    /**
     * 获得maven 项目
     */
    private List<MavenProject> getAllMavenProject(Project project) {
        MavenProjectsManager mavenProjectsManager = ServiceManager.getService(project, MavenProjectsManager.class);
        return Objects.nonNull(mavenProjectsManager) ? mavenProjectsManager.getProjects() : null;
    }

    private void info() {
        // Messages.showMessageDialog(allArtifactIds.stream().collect(Collectors.joining(",")), "提示", Messages.getInformationIcon());
        // NotificationGroup group = com.intellij.notification.NotificationGroup#createNotification$default(com.intellij.notification.NotificationGroup, java.lang.String, java.lang.String, com.intellij.notification.NotificationType, com.intellij.notification.NotificationListener, int, java.lang.Object)
    }

    private void notifyInfo(String info) {
        NotificationGroupManager groupManager = NotificationGroupManager.getInstance();
        Notification notification = groupManager.getNotificationGroup("DEMO Notification Group")
                .createNotification(info, MessageType.INFO);
        Notifications.Bus.notify(notification);
    }
}
